import config from "@/config/index.js";
import * as imageConversion from 'image-conversion'
import fly from "flyio"

export default function(callback) {
	let promise;
	if (typeof callback !== 'function') {
		promise = new Promise((resolve, reject) => {
			callback = function(resolvedata, rejectdata) {
				if (resolvedata) {
					resolve(resolvedata);
				} else {
					reject(rejectdata);
				}
			};
		});
	}

	uni.chooseImage({
		success: (chooseImageRes) => {
			const data = chooseImageRes.tempFiles[0];
			imageConversion
				.compressAccurately(data, 100)
				.then(async (res) => {
					let formData = new FormData()
					formData.append('pic', res, name.name)
					try {
						uni.showLoading({
							title: '加载中'
						});

						let reqdata = await fly.post(config.baseURL + "/UploadGetVin", formData, {
							headers: {
								"content-type": "multipart/form-data"
							}
						})
						uni.hideLoading()
						callback(JSON.parse(reqdata.data))
					} catch (e) {
						//TODO handle the exception
						uni.hideLoading();
						console.error('请求失败:', error);
					}

				})

		}
	});

	return promise;
}